package kassiopeia.plugin.crawler.filter;

import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import kassiopeia.plugin.Event;
import kassiopeia.plugin.ProcessorPlugin;

import org.apache.log4j.Logger;

public class SeenUrlFilterPlugin extends ProcessorPlugin {

    private final String OUTPUT = "out";
    private final Logger logger = Logger.getLogger(SeenUrlFilterPlugin.class);

    private Set<URL> seenUrls = new HashSet<URL>();

    @Override
    public void beforeTask() {
        logger.info("SeenUrlFilterPlugin plugin constructed : " + getContext().getPluginInstanceId());
    }

    @Override
    public void process(Event event) {
        URL url = getNotNullableEventBody(event, URL.class);

        synchronized (seenUrls) {
            if(!wasUrlSees(url)) {
                logger.debug("Accepting event for url: " + url);
                seenUrls.add(url);
                getContext().sendLocally(event, OUTPUT);
            } else {
                logger.debug("Already seen url: " + url);
            }
        }
    }

    private boolean wasUrlSees(URL url) {
        Iterator<URL> iterator = seenUrls.iterator();
        while(iterator.hasNext()) {
            URL seenUrl = iterator.next();
            if(seenUrl.sameFile(url)) {
                return true;
            }
        }
        return false;
    }
}
